from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QTableWidgetItem, QApplication
from PyQt5.QtCore import QTranslator
from classes.engine import Engine
from ui_py.page3_ui import Main_Ui
from temp import Excel, ReadExcel
from expert import Expert
import json
from validation import validation
from error import Error
from dataExpert import Data
class Page3(QtWidgets.QWidget, Main_Ui):
    engine = Engine()
    def __init__(self):
        QtWidgets.QWidget.__init__(self)
        self.setupUi(self)
        self.expert = Expert()
        self.err = Error()
        self.data = Data()
        with open('lang.json', encoding='utf-8') as file:
            data = json.load(file)
        if data['lang'] != "":
            self.trans = QTranslator(self)
            self.trans.load('translate/' + 'page3')
            _app = QApplication.instance() # получить экземпляр приложения
            _app.installTranslator(self.trans)
        self.err.clear = 0
        if self.engine.cil == "L6" or self.engine.cil == "V12":
            n = 8
        elif self.engine.cil == "L4" or self.engine.cil == "V8":
            n = 6
        else:
            n = 5
        self.tableWidget.blockSignals(True)
        for i in range(1, n):
            row = self.tableWidget.rowCount()
            self.tableWidget.insertRow(row)
            if data['lang'] != "":
                page = data['en']['page3']
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem(page['table'][0].format(i, i)))
            else:
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem("Угол начала канавки MN{0}; MN{1}01".format(i, i)))
            try:
                value = str(self.data.excel['MN{1}01'.format(i, i)])
            except:
                value = ""
            self.tableWidget.setItem(row, 0, QTableWidgetItem(value))
            if data['lang'] != "":
                self.tableWidget.setItem(row, 1, QTableWidgetItem("deg"))
            else:
                self.tableWidget.setItem(row, 1, QTableWidgetItem("град"))
            row = self.tableWidget.rowCount()
            self.tableWidget.insertRow(row)
            if data['lang'] != "":
                page = data['en']['page3']
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem(page['table'][1].format(i, i)))
            else:
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem("Угол окончания канавки MN{0}; MN{1}02".format(i, i)))
            try:
                value = str(self.data.excel['MN{1}02'.format(i, i)])
            except:
                value = ""
            self.tableWidget.setItem(row, 0, QTableWidgetItem(value))
            if data['lang'] != "":
                self.tableWidget.setItem(row, 1, QTableWidgetItem("deg"))
            else:
                self.tableWidget.setItem(row, 1, QTableWidgetItem("град"))
        self.tableWidget.blockSignals(False)
        self.tableWidget.cellChanged.connect(self.cellChange)
    def cellChange(self, row, column):
        key = self.tableWidget.verticalHeaderItem(row).text().split('; ')[1]
        if validation(self.tableWidget.item(row, column).text(), key, self.data.excel):
            self.tableWidget.blockSignals(True)
            self.tableWidget.item(row, column).setBackground(QtGui.QColor(112, 255, 127))
            self.tableWidget.blockSignals(False)
            self.expert.excel[key] = self.tableWidget.item(row, column).text()
            self.data.excel[key] = self.tableWidget.item(row, column).text()
            # excel = ReadExcel(self.filename)
            # excel.updateCell(key, self.tableWidget.item(row, column).text(), self.filename)
            try:
                if self.err.error_dict[key] > 0:
                    del self.err.error_dict[key]
            except:
                pass
        else:
            self.tableWidget.blockSignals(True)
            self.tableWidget.item(row, column).setBackground(QtGui.QColor(255, 152, 152))
            self.tableWidget.blockSignals(False)
            self.err.error_dict[key] = 3
            self.err.page = 3
    # def press(self, row, column):
    #     self.tableWidget.cellChanged.connect(self.cellChange)